<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人草稿列表页</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <script src="js/jquery.min.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo.jpg" alt="">
        <span class="title" style="font-size:30px">优文共享社区</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="myblog_list.html">个人主页</a>
        <a href="blog_list.html">文章展示页</a>
        <a href="blog_add.html">写文章</a>
        <a href="javascript:logout()">注销</a>
        <!-- <a href="#">注销</a> -->
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="container-left">
            <div class="card">
                <div id="imgadd"></div>
                <h3 id="username"></h3>
                <!-- <a href="http:www.github.com">座右铭</a> -->
                <div class="counter">
                    <span>草稿数</span>
                    <!-- <span>分类</span> -->
                </div>
                <div class="counter">
                    <span id="artCount"></span>
                </div>
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div id="artDiv" class="container-right">

        </div>
    </div>

    <script>
        //获取左侧个人信息
        function showInfo() {
            jQuery.ajax({
                url: "/user/showinfosave",
                type: "POST",
                data: {},
                success: function (result) {
                    if (result != null && result.code == 200) {
                        jQuery("#username").text(result.data.username);
                        jQuery("#artCount").text(result.data.artCount);
                    } else {
                        alert("用户信息加载失败！请刷新重试！");
                    }
                }
            });
        }
        showInfo();

        // 获取我的文章列表数据
        function getMyArtList() {
            jQuery.ajax({
                url: "/art/mylistsave",
                type: "POST",
                data: {},
                success: function (result) {
                    if (result != null && result.code == 200) {
                        // 有两种情况，一种是发表了文章，一种是没有发表任何文章 
                        if (result.data != null && result.data.length > 0) {
                            // 此用户发表文章了
                            var artListDiv = "";
                            for (var i = 0; i < result.data.length; i++) {
                                var artItem = result.data[i];
                                artListDiv += '<div class="blog">';
                                artListDiv += '<div class="title">' + artItem.title + '</div>';
                                artListDiv += '<div class="date">' + artItem.updatetime + '</div>';
                                artListDiv += '<div class="desc">';
                                artListDiv += artItem.content;
                                artListDiv += '</div>';
                                artListDiv += '<a href="blog_edit.html?id=' +
                                    artItem.id + '" class="detail">修改 &gt;&gt;</a>&nbsp;&nbsp;';
                                artListDiv += '<a href="javascript:myDel(' +
                                    artItem.id + ');" class="detail">删除 &gt;&gt;</a>';
                                artListDiv += '</div>';
                            }
                            jQuery("#artDiv").html(artListDiv);
                        } else {
                            // 当前用户从未发过任何文章
                            jQuery("#artDiv").html("<h3>暂无草稿~</h3>");
                        }
                    } else {
                        alert("查询文章列表出错，请重试！");
                    }
                }
            });
        }
        getMyArtList();

        // 删除文章
        function myDel(id) {
            if (confirm("确认删除草稿？")) {
                //删除文章
                jQuery.ajax({
                    url: "/art/del",
                    type: "POST",
                    data: { "id": id },
                    success: function (result) {
                        if (result != null && result.code == 200 && result.data == 1) {
                            alert("删除草稿成功！");
                            //刷新当前页面
                            location.href = location.href;
                        } else {
                            alert("删除失败！请重试！")
                        }
                    }
                });
            }
        }

        //注销操作（退出登陆状态）
        function logout() {
            if (confirm("确认退出登录？")) {
                jQuery.ajax({
                    url: "/user/logout",
                    type: "POST",
                    data: {},
                    success: function (result) {
                        if (result != null && result.code == 200) {
                            location.href = "/login.html";
                        }
                    }
                });
            }
        }

        //获取头像
        function getimage() {
            jQuery.ajax({
                url: "/user/getimg",
                type: "POST",
                data: {},
                success: function (result) {
                    if (result != null && result.code == 200) {
                        jQuery("#imgadd").html('<img src="img/' + result.data + '" class="avtar" alt="">');
                    } else {
                        jQuery("#imgadd").html('<img src="img/no.jpg" class="avtar" alt="">');
                    }
                }
            });
        }
        getimage();
    </script>
</body>

</html>